<?php

header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
require("../config.php");
include_once("functii.php");

function exista_actiune($obiect, $planeta) {
	$verificare = mysql_num_rows(mysql_query("SELECT obiect FROM actiuni_locale WHERE planeta = '".$planeta."' AND obiect = '".$obiect."' AND tip != 'construire_unitati'"));
	 if ($verificare > 0)
	 return 1;
	return 0;
}

if(isset($_GET['id'], $_GET['cerere'], $_GET['obiect'], $_GET['santier'])) 
 {
   $id = addentities($_GET['id']);
   $obiect_nume = addentities($_GET['obiect']);
   $santier = addentities($_GET['santier']);
   $cerere = addentities($_GET['cerere']);
  
   $nr_actiuni_curente = mysql_num_rows(mysql_query("SELECT id FROM actiuni_locale WHERE planeta = '".$planeta."'"));
   $minerale = valoare_mysql("SELECT minerale FROM planete WHERE id = '$planeta'");
   $gaz = valoare_mysql("SELECT gaz FROM planete WHERE id = '$planeta'");
   $constante = mysql_fetch_array(mysql_query("SELECT * FROM c_cladiri WHERE nume = '$obiect_nume'"));
   $obiect = $constante["id"];
   $timp_constr = $constante["timp_constr"];
   $formula_inc_nivel = $constante["formula_inc_nivel"];
   $form_inc_minerale = $constante["form_inc_minerale"];
   $form_inc_gaz = $constante["form_inc_gaz"];
   $form_inc_populatie = $constante["form_inc_populatie"];
   $cost_minerale = $constante["cost_minerale"];
   $cost_gaz = $constante["cost_gaz"];
   $cost_populatie = $constante["cost_populatie"];
   $nivel = valoare_mysql("SELECT nivel FROM cladiri WHERE nume = '$obiect' AND planeta = '$planeta'");
   $id_unic = md5(time().$obiect);
   $status = "Hello world from actiuni.php";
   $nr_max = 2; //Nr. maxim de actiuni ce pot fi executate concomitent.
   
   switch($cerere)
	{
	  case "upgrade":
	  if ($nr_actiuni_curente < $nr_max) {	 
	  $query = mysql_query("SELECT id FROM cladiri WHERE nume = '$obiect' AND planeta = '$planeta'");  
	   if ($query != "" && !exista_actiune($obiect, $planeta)) {
	    //Calculare valori pentru incrementare
	    $id_unic = md5(time().$obiect);
        $nivel_upgrade = $nivel+1;
	    $durata = formula_incrementare($timp_constr, $nivel, $formula_inc_nivel, "");
		$populatie = round(formula_incrementare($cost_populatie, $nivel, $form_inc_populatie, ""));
		//Incrementare valori in tabelul users, coloana populatie
		incrementare_populatie($populatie, $user, 'inserare');
	    //Trimitere cereri in baza de date
		mysql_query("UPDATE unitati SET nr = nr + '".$populatie."' WHERE planeta = '".$planeta."' AND locatie = 'infrastructura' AND tip = '0'");
	    mysql_query("INSERT INTO actiuni_locale (id, tip, planeta, durata, incepere, santier, obiect, valoare) VALUES ('$id_unic', '$cerere', '$planeta', '$durata', NOW( ), '$santier', '$obiect', '$nivel_upgrade')"); 
	   $a = round(formula_incrementare($cost_minerale, $nivel, $form_inc_minerale, ""), -1);
       $b = round(formula_incrementare($cost_gaz, $nivel, $form_inc_gaz, ""), -1);
       mysql_query("UPDATE planete SET minerale = minerale - '".$a."' , gaz = gaz - '".$b."' WHERE id = '".$planeta."'"); 
	   } 
	   else
	    $status = "Upgrade-ul nu s-a putut efectua.";
	  } else $status = "max ".$nr_max;	
	  break;
	  
	  case "construire":
	  if ($nr_actiuni_curente < $nr_max) {	
	  if (!exista_actiune($obiect, $planeta)) {
	  //Calculare valori pentru incrementare
	   $a = round(formula_incrementare($cost_minerale, 1, $form_inc_minerale, ""), -1);
       $b = round(formula_incrementare($cost_gaz, 1, $form_inc_gaz, ""), -1);
	   $populatie = round(formula_incrementare($cost_populatie, $nivel, $form_inc_populatie, ""));
		//Incrementare valori in tabelul users, coloana populatie
		incrementare_populatie($populatie, $user, 'inserare');
	    //Trimitere cereri in baza de date
	   mysql_query("UPDATE unitati SET nr = nr + '".$populatie."' WHERE planeta = '".$planeta."' AND locatie = 'infrastructura'");
       mysql_query("UPDATE planete SET minerale = minerale - '".$a."' , gaz = gaz - '".$b."' WHERE id = '".$planeta."'");
	   $durata = $timp_constr;
	   mysql_query("INSERT INTO actiuni_locale (id, tip, planeta, durata, incepere, santier, obiect, valoare) VALUES ('$id_unic', '$cerere', '$planeta', '$durata', NOW( ), '$santier', '$obiect', '$id')");
       } else $status = "Actiunea nu este disponibila momentan";
	  } else $status = "max ".$nr_max;
	  break;
	  
	  case "anulare": 
	  $x = mysql_fetch_row(mysql_query("SELECT santier, obiect, tip FROM actiuni_locale WHERE id = '$id' AND planeta = '$planeta'"));
	  //Setare variabile
	  $santier = $x[0];
	  $obiect = $x[1];
	  $tip = $x[2];
	  $query = mysql_num_rows(mysql_query("SELECT obiect FROM actiuni_locale WHERE id = '$id' AND planeta = '$planeta'"));
	    if ($query > 0) {
	   $y = mysql_query("DELETE FROM actiuni_locale WHERE planeta = '$planeta' AND id = '$id'");
	   if ($tip == "construire")
	    mysql_query("DELETE FROM cladiri WHERE planeta = '$planeta' AND santier = '$santier'");

	   if ($tip == 'demolare') {
	    //Calculare valori decrementare
        $populatie = round(formula_incrementare($cost_populatie, $nivel-1, $form_inc_populatie, ""));
	    $a = round(formula_incrementare($cost_minerale, $nivel-1, $form_inc_minerale, ""), -1)/2;
        $b = round(formula_incrementare($cost_gaz, $nivel-1, $form_inc_gaz, ""), -1)/2;
	    //Incrementare valori in tabelul users, coloana populatie
		incrementare_populatie($populatie, $user, 'inserare');
		mysql_query("UPDATE unitati SET nr = nr + '".$populatie."' WHERE planeta = '".$planeta."' AND locatie = 'infrastructura'");
		mysql_query("UPDATE planete SET minerale = minerale - '".$a."' , gaz = gaz - '".$b."' WHERE id = '".$planeta."'");
	   } else {
	   	//Calculare valori decrementare
        $populatie = round(formula_incrementare($cost_populatie, $nivel, $form_inc_populatie, ""));
	    $a = round(formula_incrementare($cost_minerale, $nivel, $form_inc_minerale, ""), -1);
        $b = round(formula_incrementare($cost_gaz, $nivel, $form_inc_gaz, ""), -1);
	    //Incrementare valori in tabelul users, coloana populatie
		incrementare_populatie($populatie, $user, 'scadere');
		mysql_query("UPDATE unitati SET nr = nr - '".$populatie."' WHERE planeta = '".$planeta."' AND locatie = 'infrastructura'");
		mysql_query("UPDATE planete SET minerale = minerale + '".$a."' , gaz = gaz + '".$b."' WHERE id = '".$planeta."'");
	   } 
       if($y != "") 
			$status = "Actiunea a fost anulata.";
		}
	  break;
	  
	  case "demolare":
	  if ($nr_actiuni_curente < $nr_max) {	
	  $query = mysql_num_rows(mysql_query("SELECT nume FROM cladiri WHERE nume = '$obiect' AND planeta = '$planeta'"));  
	   if ($query > 0 && !exista_actiune($obiect, $planeta)) {
       $x = $nivel-1;
	   $durata = formula_incrementare("100", $nivel, "0.1", "");	   
	   //Calculare valori incrementare
	   $a = round(formula_incrementare($cost_minerale, $x, $form_inc_minerale, ""), -1)/2;
       $b = round(formula_incrementare($cost_gaz, $x, $form_inc_gaz, ""), -1)/2;
	   //Incrementare valori in tabelul users, coloana populatie
 	   $populatie = round(formula_incrementare($cost_populatie, $x, $form_inc_populatie, ""));
	   incrementare_populatie($populatie, $user, 'scadere');
	   //Trimitere cereri in baza de date
	   mysql_query("UPDATE unitati SET nr = nr - '".$populatie."' WHERE planeta = '".$planeta."' AND locatie = 'infrastructura'");
	   mysql_query("UPDATE planete SET minerale = minerale + '".$a."' , gaz = gaz + '".$b."' WHERE id = '".$planeta."'");
	   mysql_query("INSERT INTO actiuni_locale (id, tip, planeta, durata, incepere, santier, obiect, valoare) VALUES ('$id_unic', '$cerere', '$planeta', '$durata', NOW( ), '$santier', '$obiect', '$x')"); 
	   } else $status = "Actiunea nu este disponibila momentan";
	  } else $status = "max ".$nr_max;
	  break;
	  
	  case "construire_unitati":
	   
	  break;
	}
	echo $status;
	
 } else echo "Parametrii nu sunt corecti.";

mysql_close($db);

?>